#include <vector>
#include <queue>

using namespace std;
using int64 = long long;

class Solution {
public:
    int magicTower(vector<int>& nums) {
        int64 i = 0, hp = 1, ans = 0, size = nums.size();
        priority_queue<int, vector<int>, greater<int>> pq;
        for (; i < nums.size(); ++i) {
            if (i >= (size << 1)) return -1;
            hp += nums[i];
            if (nums[i] < 0) pq.emplace(nums[i]);
            while (hp <= 0) {
                if (pq.empty()) return -1;
                hp -= pq.top();
                ++ans;
                nums.emplace_back(pq.top());
                pq.pop();
            }
        }
        return ans;
    }
};